package com.itcast.security.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.itcast.common.model.dto.request.SetmealCategoryAddRepDTO;
import com.itcast.common.model.dto.request.SetmealCategoryReqDTO;
import com.itcast.common.model.dto.response.SetmealCategoryResDTO;
import com.itcast.security.mapper.SetmealCategoryMapper;
import com.itcast.security.model.SetmealCategory;
import com.itcast.security.service.ISetmealCategoryService;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.mysql.utils.PageHelperUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

/**
* <p>
* setmeal_category Service 接口实现
* </p>
*
* @author Administrator
* @since 2024-11-05 16:09:32
*/
@Service
@Transactional
@Slf4j
public class SetmealCategoryServiceImpl extends ServiceImpl<SetmealCategoryMapper, SetmealCategory> implements ISetmealCategoryService {

    @Autowired
    private SetmealCategoryMapper setmealCategoryMapper;

    /**
     * 分页查询
     *
     * @param setmealCategoryReqDTO
     * @return
     */
    @Override
    public PageResult<SetmealCategoryResDTO> querySetmealCategoryList(SetmealCategoryReqDTO setmealCategoryReqDTO) {
        return PageHelperUtils.selectPage(setmealCategoryReqDTO,
                () -> baseMapper.querySetmealCategoryList(setmealCategoryReqDTO.getName()));
    }


    /**
     * 新增套餐类型
     *
     * @param setmealCategoryAddRepDTO
     */
    @Override
    public void saveSetmealCategory(SetmealCategoryAddRepDTO setmealCategoryAddRepDTO) {
        String setmealCategoryName = setmealCategoryAddRepDTO.getSetmealCategoryName();
        Long count = lambdaQuery().eq(SetmealCategory::getSetmealCategoryName, setmealCategoryName).count();
        if (count > 0) {
            throw new RuntimeException("套餐类型已存在");
        }
        SetmealCategory setmealCategory = new SetmealCategory();
        setmealCategory.setSetmealCategoryName(setmealCategoryAddRepDTO.getSetmealCategoryName());
        setmealCategory.setSetmealCategorySortNum(setmealCategoryAddRepDTO.getSetmealCategorySortNum());
        setmealCategory.setCreateTime(LocalDateTime.now());

        //插入数据库
        int insert = baseMapper.insert(setmealCategory);
        if (insert <= 0) {
            throw new RuntimeException("添加套餐类型失败");
        }
    }

    /**
     * 根据id查询套餐类型
     *
     * @param id
     * @return
     */
    @Override
    public SetmealCategoryResDTO getByIdSetmeal(Long id) {
        SetmealCategory byId = getById(id);
        SetmealCategoryResDTO setmealCategoryResDTO = BeanUtil.copyProperties(byId, SetmealCategoryResDTO.class);
        return setmealCategoryResDTO;
    }


    /**
     * 修改套餐类型
     *
     * @param id
     * @param setmealCategoryAddRepDTO
     */
    @Override
    public void updateByIdSMC(Long id, SetmealCategoryAddRepDTO setmealCategoryAddRepDTO) {
        SetmealCategory setmealCategory = getById(id);
        if (ObjectUtil.isNull(setmealCategory)) {
            throw new RuntimeException("套餐类型不存在");
        }
        //删除
        boolean b = removeById(id);
        if (!b) {
            throw new RuntimeException("删除套餐类型失败");
        }
        saveSetmealCategory(setmealCategoryAddRepDTO);
    }

    /**
     * 启用套餐类型
     *
     * @param id
     */
    @Override
    public void enable(Long id) {
        SetmealCategory byId = getById(id);
        if (ObjectUtil.isNull(byId)) {
            throw new RuntimeException("套餐类型不存在");
        }
        Integer setmealCategoryStatus = byId.getSetmealCategoryStatus();
        if (setmealCategoryStatus == 1) {
            throw new RuntimeException("套餐类型已启用");
        }
        byId.setSetmealCategoryStatus(1);
        boolean update = updateById(byId);
        if (!update) {
            throw new RuntimeException("启用套餐类型失败");
        }
    }

    /**
     * 删除套餐类型
     *
     * @param id
     */
    @Override
    public void removeSetmealCategory(Long id) {
        SetmealCategory setmealCategory = getById(id);
        if (ObjectUtil.isNull(setmealCategory)) {
            throw new RuntimeException("套餐类型不存在");
        }
        Integer setmealCategoryStatus = setmealCategory.getSetmealCategoryStatus();
        if (setmealCategoryStatus != 0) {
            throw new RuntimeException("套餐类型无法删除");
        }
        boolean removeById = removeById(id);
        if (!removeById) {
            throw new RuntimeException("删除套餐类型失败");
        }
    }

    @Override
    public List<SetmealCategoryResDTO> listSetmeal() {
        return this.baseMapper.getAll();
    }
}